package com.maxlen.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.to.MemberPrice;
import com.maxlen.common.to.SkuReductionTO;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.coupon.dao.SkuFullReductionDao;
import com.maxlen.coupon.entity.MemberPriceEntity;
import com.maxlen.coupon.entity.SkuFullReductionEntity;
import com.maxlen.coupon.entity.SkuLadderEntity;
import com.maxlen.coupon.service.MemberPriceService;
import com.maxlen.coupon.service.SkuFullReductionService;
import com.maxlen.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveDetail(SkuReductionTO skuReductionTO) {
        // 保存 sku 的优惠、满减等信息 maxlen-sms -> sms_sku_ladder \ sms_sku_full_reduction \ sms_member_price
        if(ObjectUtils.isEmpty(skuReductionTO)){
            return;
        }

        // sms_sku_ladder
        if(skuReductionTO.getFullCount() > 0 || skuReductionTO.getDiscount().compareTo(new BigDecimal("0")) == 1){
            SkuLadderEntity skuLadderEntity = CopyUtils.copy(skuReductionTO, SkuLadderEntity.class);
            skuLadderEntity.setAddOther(skuReductionTO.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // sms_sku_full_reduction
        if(skuReductionTO.getFullPrice().compareTo(new BigDecimal("0")) == 1 || skuReductionTO.getReducePrice().compareTo(new BigDecimal("0")) == 1){
            SkuFullReductionEntity skuFullReductionEntity = CopyUtils.copy(skuReductionTO, SkuFullReductionEntity.class);
            skuFullReductionEntity.setAddOther(skuReductionTO.getPriceStatus());
            baseMapper.insert(skuFullReductionEntity);
        }

        // sms_member_price
        List<MemberPrice> memberPrice = skuReductionTO.getMemberPrice();
        if(memberPrice!= null && !memberPrice.isEmpty()){
            List<MemberPriceEntity> memberPriceEntityList = memberPrice.stream()
                    .filter(item -> {
                        return item.getPrice().compareTo(new BigDecimal("0")) == 1;
                    })
                    .map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTO.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntityList);
        }
    }

}
